import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';
import { BaseUI } from '../../../common/baseUI';

@IonicPage()
@Component({
  selector: 'page-register',
  templateUrl: 'register.html',
})
export class RegisterPage extends BaseUI {

  isShow: Boolean = true; //判断是否显示倒计时
  count: number = 60; // 倒计时时间
  timer: any = null ; // 定时器的名称
  tel: string = ''; // 双向绑定手机号码
  code: number; // 验证码
  radom = Math.floor(Math.random()*100);
  password: string = '';
  isEye: boolean = true;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public toastCtrl: ToastController) {
    super();
  }

  ionViewDidLoad() {
  }

  setCode() {    
    const reg = /^1[34578][0-9]{9}$/;
    if (reg.test(this.tel)) {
      this.isShow = false;
      this.setTiem();
      console.log(this.radom);
    }else {
      this.showToast(this.toastCtrl,'手机格式不正确');
    }

  }

  // 设计定时器
  setTiem() {
    clearInterval(this.timer);
    this.timer = setInterval( () => {
      this.count--;
      if (this.count === 0) {
        this.count = 60;
        this.isShow = true;
        clearInterval(this.timer);
      }
    }, 1000);
  }

  gotoUpDataInfo() {
    // 正则验证手机号
    const reg = /^1[34578][0-9]{9}$/;
    if (reg.test(this.tel)) {
      console.log(this.tel);
      if (this.code === 0) {
        if (this.password.length>=8&& this.password.length<=16) {
          this.navCtrl.push('UpdataInfoPage', {tel:this.tel});
        }else if(this.password.length<8){
          this.showToast(this.toastCtrl, '密码长度需要大于8位');
        }else {
          this.showToast(this.toastCtrl, '密码长度需要小于16位');
        }
      }else {
        this.showToast(this.toastCtrl, '验证码错误');  
      }
    }else {
      this.showToast(this.toastCtrl, '请填写手机');
    }
  }

  blurInput() {
    const reg = /^1[34578][0-9]{9}$/;
    if (!reg.test(this.tel)) {
      this.showToast(this.toastCtrl,'手机格式不正确');
    }else if (1) {
      // TODO: 请求接口 判断是否注册
    }
  }
  
  // 是否显示密码
  isShowPassword() {
    this.isEye = !this.isEye;
    console.log(this.isEye)
  }
}
